package com.shoufeng.demo.service;

import com.shoufeng.demo.server.HealthServer;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StartServiceImpl implements StartService{

  private static final Logger LOGGER = LoggerFactory.getLogger(StartServiceImpl.class);

  @Autowired
  private HealthServer healthServer;

  @Override
  @PostConstruct
  public void startHealthServer() {
    LOGGER.info("开始启动服务");
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.execute(() -> {
      try {
        healthServer.start();
        healthServer.awaitTermination();
      } catch (IOException e) {
        LOGGER.error("启动服务失败: " + e.getMessage());
      } catch (InterruptedException e) {
        LOGGER.error("服务中断: " + e.getMessage());
      }
    });
    executorService.shutdown();
  }

  @Override
  public void startHealthClient() {

  }
}
